package cn.iocoder.yudao.module.cont.service.base;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.cont.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.BaseInfoDO;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.DetailInfoDO;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.MaterialInfoDO;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.PayInfoDO;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.ChangeInfoDO;
import cn.iocoder.yudao.module.cont.dal.dataobject.base.PlanInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.cont.dal.mysql.base.BaseInfoMapper;
import cn.iocoder.yudao.module.cont.dal.mysql.base.DetailInfoMapper;
import cn.iocoder.yudao.module.cont.dal.mysql.base.MaterialInfoMapper;
import cn.iocoder.yudao.module.cont.dal.mysql.base.PayInfoMapper;
import cn.iocoder.yudao.module.cont.dal.mysql.base.ChangeInfoMapper;
import cn.iocoder.yudao.module.cont.dal.mysql.base.PlanInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cont.enums.ErrorCodeConstants.*;

/**
 * 合同基础信息表  Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BaseInfoServiceImpl implements BaseInfoService {

    @Resource
    private BaseInfoMapper baseInfoMapper;
    @Resource
    private DetailInfoMapper detailInfoMapper;
    @Resource
    private MaterialInfoMapper materialInfoMapper;
    @Resource
    private PayInfoMapper payInfoMapper;
    @Resource
    private ChangeInfoMapper changeInfoMapper;
    @Resource
    private PlanInfoMapper planInfoMapper;

    @Override
    public Long createBaseInfo(BaseInfoSaveReqVO createReqVO) {
        // 插入
        BaseInfoDO baseInfo = BeanUtils.toBean(createReqVO, BaseInfoDO.class);
        baseInfoMapper.insert(baseInfo);
        // 返回
        return baseInfo.getId();
    }

    @Override
    public void updateBaseInfo(BaseInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseInfoExists(updateReqVO.getId());
        // 更新
        BaseInfoDO updateObj = BeanUtils.toBean(updateReqVO, BaseInfoDO.class);
        baseInfoMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBaseInfo(Long id) {
        // 校验存在
        validateBaseInfoExists(id);
        // 删除
        baseInfoMapper.deleteById(id);

        // 删除子表
        deleteDetailInfoByHtid(id);
        deleteMaterialInfoByHtid(id);
        deletePayInfoByHtid(id);
        deleteChangeInfoByHtid(id);
        deletePlanInfoByHtid(id);
    }

    private void validateBaseInfoExists(Long id) {
        if (baseInfoMapper.selectById(id) == null) {
            throw exception(BASE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BaseInfoDO getBaseInfo(Long id) {
        return baseInfoMapper.selectById(id);
    }

    @Override
    public PageResult<BaseInfoDO> getBaseInfoPage(BaseInfoPageReqVO pageReqVO) {
        return baseInfoMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（建设合同详细信息） ====================

    @Override
    public PageResult<DetailInfoDO> getDetailInfoPage(PageParam pageReqVO, String htid) {
        return detailInfoMapper.selectPage(pageReqVO, htid);
    }

    @Override
    public Long createDetailInfo(DetailInfoDO detailInfo) {
        detailInfoMapper.insert(detailInfo);
        return detailInfo.getId();
    }

    @Override
    public void updateDetailInfo(DetailInfoDO detailInfo) {
        // 校验存在
        validateDetailInfoExists(detailInfo.getId());
        // 更新
        detailInfoMapper.updateById(detailInfo);
    }

    @Override
    public void deleteDetailInfo(Long id) {
        // 校验存在
        validateDetailInfoExists(id);
        // 删除
        detailInfoMapper.deleteById(id);
    }

    @Override
    public DetailInfoDO getDetailInfo(Long id) {
        return detailInfoMapper.selectById(id);
    }

    private void validateDetailInfoExists(Long id) {
        if (detailInfoMapper.selectById(id) == null) {
            throw exception(DETAIL_INFO_NOT_EXISTS);
        }
    }

    private void deleteDetailInfoByHtid(Long htid) {
        detailInfoMapper.deleteByHtid(htid);
    }

    // ==================== 子表（合同资料表 ） ====================

    @Override
    public PageResult<MaterialInfoDO> getMaterialInfoPage(PageParam pageReqVO, String htid) {
        return materialInfoMapper.selectPage(pageReqVO, htid);
    }

    @Override
    public Long createMaterialInfo(MaterialInfoDO materialInfo) {
        materialInfoMapper.insert(materialInfo);
        return materialInfo.getId();
    }

    @Override
    public void updateMaterialInfo(MaterialInfoDO materialInfo) {
        // 校验存在
        validateMaterialInfoExists(materialInfo.getId());
        // 更新
        materialInfoMapper.updateById(materialInfo);
    }

    @Override
    public void deleteMaterialInfo(Long id) {
        // 校验存在
        validateMaterialInfoExists(id);
        // 删除
        materialInfoMapper.deleteById(id);
    }

    @Override
    public MaterialInfoDO getMaterialInfo(Long id) {
        return materialInfoMapper.selectById(id);
    }

    private void validateMaterialInfoExists(Long id) {
        if (materialInfoMapper.selectById(id) == null) {
            throw exception(MATERIAL_INFO_NOT_EXISTS);
        }
    }

    private void deleteMaterialInfoByHtid(Long htid) {
        materialInfoMapper.deleteByHtid(htid);
    }

    // ==================== 子表（合同收付款记录表 ） ====================

    @Override
    public PageResult<PayInfoDO> getPayInfoPage(PageParam pageReqVO, String htid) {
        return payInfoMapper.selectPage(pageReqVO, htid);
    }

    @Override
    public Long createPayInfo(PayInfoDO payInfo) {
        payInfoMapper.insert(payInfo);
        return payInfo.getId();
    }

    @Override
    public void updatePayInfo(PayInfoDO payInfo) {
        // 校验存在
        validatePayInfoExists(payInfo.getId());
        // 更新
        payInfoMapper.updateById(payInfo);
    }

    @Override
    public void deletePayInfo(Long id) {
        // 校验存在
        validatePayInfoExists(id);
        // 删除
        payInfoMapper.deleteById(id);
    }

    @Override
    public PayInfoDO getPayInfo(Long id) {
        return payInfoMapper.selectById(id);
    }

    private void validatePayInfoExists(Long id) {
        if (payInfoMapper.selectById(id) == null) {
            throw exception(PAY_INFO_NOT_EXISTS);
        }
    }

    private void deletePayInfoByHtid(Long htid) {
        payInfoMapper.deleteByHtid(htid);
    }

    // ==================== 子表（合同变更记录表 ） ====================

    @Override
    public PageResult<ChangeInfoDO> getChangeInfoPage(PageParam pageReqVO, String htid) {
        return changeInfoMapper.selectPage(pageReqVO, htid);
    }

    @Override
    public Long createChangeInfo(ChangeInfoDO changeInfo) {
        changeInfoMapper.insert(changeInfo);
        return changeInfo.getId();
    }

    @Override
    public void updateChangeInfo(ChangeInfoDO changeInfo) {
        // 校验存在
        validateChangeInfoExists(changeInfo.getId());
        // 更新
        changeInfoMapper.updateById(changeInfo);
    }

    @Override
    public void deleteChangeInfo(Long id) {
        // 校验存在
        validateChangeInfoExists(id);
        // 删除
        changeInfoMapper.deleteById(id);
    }

    @Override
    public ChangeInfoDO getChangeInfo(Long id) {
        return changeInfoMapper.selectById(id);
    }

    private void validateChangeInfoExists(Long id) {
        if (changeInfoMapper.selectById(id) == null) {
            throw exception(CHANGE_INFO_NOT_EXISTS);
        }
    }

    private void deleteChangeInfoByHtid(Long htid) {
        changeInfoMapper.deleteByHtid(htid);
    }

    // ==================== 子表（合同收付款计划表 ） ====================

    @Override
    public PageResult<PlanInfoDO> getPlanInfoPage(PageParam pageReqVO, String htid) {
        return planInfoMapper.selectPage(pageReqVO, htid);
    }

    @Override
    public Long createPlanInfo(PlanInfoDO planInfo) {
        planInfoMapper.insert(planInfo);
        return planInfo.getId();
    }

    @Override
    public void updatePlanInfo(PlanInfoDO planInfo) {
        // 校验存在
        validatePlanInfoExists(planInfo.getId());
        // 更新
        planInfoMapper.updateById(planInfo);
    }

    @Override
    public void deletePlanInfo(Long id) {
        // 校验存在
        validatePlanInfoExists(id);
        // 删除
        planInfoMapper.deleteById(id);
    }

    @Override
    public PlanInfoDO getPlanInfo(Long id) {
        return planInfoMapper.selectById(id);
    }

    private void validatePlanInfoExists(Long id) {
        if (planInfoMapper.selectById(id) == null) {
            throw exception(PLAN_INFO_NOT_EXISTS);
        }
    }

    private void deletePlanInfoByHtid(Long htid) {
        planInfoMapper.deleteByHtid(htid);
    }

}